﻿using SqlScriptVault.Model;
using SqlScriptVault.View;
using SqlScriptVault.ViewModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SqlScriptVault.Services
{
    public class MessageDisplayService : IMessageDisplayService
    {
        public void ShowErrorMessage(string message)
        {
            MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public MessageBoxResult ShowQuestion(string message, string caption)
        {
            return MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Question);
        }

        public Script OpenScript(string fileName)
        {
            ScriptWindow scriptWindow = new ScriptWindow(fileName);

            bool? result = scriptWindow.ShowDialog();

            if (result.HasValue && result.Value)
            {
                return ((ScriptViewModel)scriptWindow.DataContext).Script;
            }

            return null;
        }

        public void OpenConnectionManager()
        {
            ConnectionManagerWindow connectionManagerWindow = new ConnectionManagerWindow();

            connectionManagerWindow.ShowDialog();
        }

        public ServerGroup OpenServerGroup()
        {
            ServerGroupWindow serverGroupWindow = new ServerGroupWindow();

            bool? result = serverGroupWindow.ShowDialog();

            if (result.HasValue && result.Value)
            {
                return ((ServerGroupViewModel)serverGroupWindow.DataContext).ServerGroup;
            }

            return null;
        }

        public Server OpenServer()
        {
            ServerWindow serverWindow = new ServerWindow();

            bool? result = serverWindow.ShowDialog();

            if (result.HasValue && result.Value)
            {
                return ((ServerViewModel)serverWindow.DataContext).Server;
            }

            return null;
        }

        public void OpenExecution(ScriptVault scriptVault)
        {
            ExecutionWindow executionWindow = new ExecutionWindow(scriptVault);

            executionWindow.ShowDialog();
        }

        public void OpenExecutionProgress(ScriptVaultExecution scriptVaultExecution, Dictionary<Server, SqlCredential> sqlCredentials)
        {
            ExecutionProgressWindow executionProgressWindow = new ExecutionProgressWindow(scriptVaultExecution, sqlCredentials);

            executionProgressWindow.ShowDialog();
        }

        public SqlCredential ShowLoginWindow(Server server)
        {
            LoginWindow loginWindow = new LoginWindow(server);

            bool? result = loginWindow.ShowDialog();

            if (result.HasValue && result.Value)
            {
                return ((LoginViewModel)loginWindow.DataContext).SqlCredential;
            }

            return null;
        }

        public void OpenHistory(ScriptVault scriptVault)
        {
            HistoryWindow historyWindow = new HistoryWindow(scriptVault);

            historyWindow.ShowDialog();
        }
    }
}
